package com.wsh.archive.service.facade;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wsh.archive.enums.TypeArchiveKey;
import com.wsh.archive.exception.ArchiveException;
import com.wsh.archive.service.business.ArchiveService;
import com.wsh.archive.service.constants.ArchiveConstants;

/**
 * Class facade the Bean get for spring configuration.
 * 
 * @author Wiltenburg, W. - <a href="wsh.cezar@gmail.com">washingtonw at wshSystems</a>
 * @since 2012, Fevereiro.
 * @version 1.0
 */
public final class BeanFacade {

	private static BeanFacade instance = null;
	
	/**
	 * Get new instance.
	 * 
	 * @return instance
	 */
	public static BeanFacade getInstance() {
		if (instance == null)
			instance = new BeanFacade();
		return instance;
	}
	
	public synchronized ArchiveService getBean(TypeArchiveKey key) throws ArchiveException {	
		
		ApplicationContext context = null;
		String bean = null;
		
		try {			
			context = new ClassPathXmlApplicationContext("applicationContext.xml");		
			bean = this.getTipoArchive(key);			
		} catch (Exception e) {
			throw new ArchiveException("O BEAN NAO EXISTE NO applicationContext.xml. ", e);
		}		
		return (ArchiveService) context.getBean(bean);
	}
	
	private String getTipoArchive(TypeArchiveKey key) {		
		String bean = ArchiveConstants.BEAN_TXT;		
		return bean;
	}
	
}